<?php
/*****************************************************************************\
+-----------------------------------------------------------------------------+
| X-Cart                                                                      |
| Copyright (c) 2001-2007 Ruslan R. Fazliev <rrf@rrf.ru>                      |
| All rights reserved.                                                        |
+-----------------------------------------------------------------------------+
| PLEASE READ  THE FULL TEXT OF SOFTWARE LICENSE AGREEMENT IN THE "COPYRIGHT" |
| FILE PROVIDED WITH THIS DISTRIBUTION. THE AGREEMENT TEXT IS ALSO AVAILABLE  |
| AT THE FOLLOWING URL: http://www.x-cart.com/license.php                     |
|                                                                             |
| THIS  AGREEMENT  EXPRESSES  THE  TERMS  AND CONDITIONS ON WHICH YOU MAY USE |
| THIS SOFTWARE   PROGRAM   AND  ASSOCIATED  DOCUMENTATION   THAT  RUSLAN  R. |
| FAZLIEV (hereinafter  referred to as "THE AUTHOR") IS FURNISHING  OR MAKING |
| AVAILABLE TO YOU WITH  THIS  AGREEMENT  (COLLECTIVELY,  THE  "SOFTWARE").   |
| PLEASE   REVIEW   THE  TERMS  AND   CONDITIONS  OF  THIS  LICENSE AGREEMENT |
| CAREFULLY   BEFORE   INSTALLING   OR  USING  THE  SOFTWARE.  BY INSTALLING, |
| COPYING   OR   OTHERWISE   USING   THE   SOFTWARE,  YOU  AND  YOUR  COMPANY |
| (COLLECTIVELY,  "YOU")  ARE  ACCEPTING  AND AGREEING  TO  THE TERMS OF THIS |
| LICENSE   AGREEMENT.   IF  YOU    ARE  NOT  WILLING   TO  BE  BOUND BY THIS |
| AGREEMENT, DO  NOT INSTALL OR USE THE SOFTWARE.  VARIOUS   COPYRIGHTS   AND |
| OTHER   INTELLECTUAL   PROPERTY   RIGHTS    PROTECT   THE   SOFTWARE.  THIS |
| AGREEMENT IS A LICENSE AGREEMENT THAT GIVES  YOU  LIMITED  RIGHTS   TO  USE |
| THE  SOFTWARE   AND  NOT  AN  AGREEMENT  FOR SALE OR FOR  TRANSFER OF TITLE.|
| THE AUTHOR RETAINS ALL RIGHTS NOT EXPRESSLY GRANTED BY THIS AGREEMENT.      |
|                                                                             |
| The Initial Developer of the Original Code is Ruslan R. Fazliev             |
| Portions created by Ruslan R. Fazliev are Copyright (C) 2001-2007           |
| Ruslan R. Fazliev. All Rights Reserved.                                     |
+-----------------------------------------------------------------------------+
\*****************************************************************************/

#
# $Id: ups.php,v 1.6.2.9 2007/09/18 08:13:49 osipov Exp $
#
# UPS - WorldShip/TrueShip module
#

if ( !defined('XCART_START') ) { header("Location: home.php"); die("Access denied"); }

#
# Format UPS shipping label
#
function func_slg_handler_UPS($order, $extra_args = null) {

	$response = array('label' => '', 'mime_type' => '', 'error' => '');

	if (empty($order) || empty($order['products'])) {
		$response['error'] = func_get_langvar_by_name("lbl_slg_order_no_products", false, false, true);
		return $response;
	}

	$stype = func_ups_check_shippingid($order['order']['shippingid']);
	if (!$stype) {
		$response['error'] = func_get_langvar_by_name("lbl_shipping_label_error",false,false,true). " (shippingid: " . $order['order']['shippingid'] . ")";
		return $response;
	}

	$delimiter = ',';

	$p_head = array();
	$strs = array();

	$hash = array();
	$hash['orderID'] = $order['order']['orderid'];
	$hash['s_company'] = $order['userinfo']['company'];
	$hash['name'] = $order['userinfo']['s_firstname']." ".$order['userinfo']['s_lastname'];
	$hash['s_email'] = $order['userinfo']['email'];
	$hash['s_phone'] = $order['userinfo']['phone'];
	$hash['s_address'] = $order['userinfo']['s_address'].($order['userinfo']['s_address_2'] ? " ".$order['userinfo']['s_address_2'] : "");
	$hash['s_city'] = $order['userinfo']['s_city'];
	$hash['s_state'] = $order['userinfo']['s_state'];
	$hash['s_zip'] = $order['userinfo']['s_zipcode'];
	$hash['s_country'] = $order['userinfo']['s_country'];
	$hash['shipmethod'] = $stype;
	$hash['insuredvalue'] = $order['order']['subtotal'];
	$hash['weight'] = 0;
	if (in_array($hash['s_country'], array("DO","PR","US"))) {
		$UPS_wunit = "LBS";
	} else {
		$UPS_wunit = "KGS";
	}
	if (!empty($order['products'])) {
		foreach($order['products'] as $p) {
			$hash['weight'] += $p['weight']*$p['amount'];
		}
		$hash['weight'] = max(0.1, func_weight_convert(func_weight_in_grams($weight), "g", (($UPS_wunit=="LBS") ? "lbs" : "kg"), 1));
	}
	$strs[] = implode($delimiter,$hash);

	# Create header
	$header = implode($delimiter,func_array_merge(array_keys($hash), $p_head))."\n";
	if (isset($extra_args) && isset($extra_args['skip_header'])) {
		$header = '';
	}

	# Create response
	$response = array(
		"label" => $header.implode("\n", $strs),
		"mime_type" => "text/csv",
		"error" => ''
	);

	return $response;
}

?>
